package com.example.mybatisdemo.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.example.mybatisdemo.mapper.UserMapper;
import com.example.mybatisdemo.model.entity.User;
import com.example.mybatisdemo.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author Asurf
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    /**
     * 登录
     * @param username 用户名
     * @param password 密码
     * @return 登录状态
     */
    @Override
    public String doLogin(String username, String password) {
// todo
//        User user = userMapper.queryUser(username);
//        if(user!=null) {
//            if(user.getPassword().equals(password)) {
//                StpUtil.login(user.getId());
//                return "登录成功！" + "用户ID为" + user.getId();
//            }
//        }
        return "登录失败";
    }

    @Override
    public String insertUser(User user) {
        if(user.getUsername()!=null) {
            int i = userMapper.insertUser(user);
            if(i!=0){
                return "添加成功";
            }
        }
        return "添加失败";
    }

    @Override
    public String deleteUser(int id) {
        int i = userMapper.deleteUser(id);
        if(i!=0){
            return "删除成功";
        }
        return "删除失败";
    }

    @Override
    public String updateUser(User user) {
        if(user.getUsername()==null){
            return "更新失败，用户名不能为空";
        }
        int i = userMapper.updateUser(user);
        if(i==0){
            return "更新失败";
        }
        return "更新成功";
    }

    @Override
    public User queryUser(int id) {
        return userMapper.queryUser(id);
    }
}
